const { ccclass, property } = cc._decorator;
import n from "./MatchGameConfig"
import l from "./MatchGameAudioMgr"
import MatchGameConfig from "./MatchGameConfig";

@ccclass
export default class CupHolderPrefab extends cc.Component {
    @property([cc.Node])
    fillSides = [];

    @property([cc.Node])
    corners = [];

    @property([cc.Node])
    outSides = [];

    @property(cc.Node)
    cupSpine = null;

    row = 0;
    col = 0;
    direArr = [];
    angleArr = [];

    showSide(e, t, a) {
        this.row = e;
        this.col = t;

        this.node.children.forEach( (e) => {
            e.active = false;
        });

        this.cupSpine.active = true;
        this.setPos(e, t, a[0]);

        var o = this.haveSamePos(this.row, this.col - 1, a);
        var i = this.haveSamePos(this.row, this.col + 1, a);
        var r = this.haveSamePos(this.row - 1, this.col, a);
        var n = this.haveSamePos(this.row + 1, this.col, a);
        var l = this.haveSamePos(this.row - 1, this.col - 1, a);
        var s = this.haveSamePos(this.row + 1, this.col - 1, a);
        var c = this.haveSamePos(this.row - 1, this.col + 1, a);
        var u = this.haveSamePos(this.row + 1, this.col + 1, a);

        this.direArr = [o, n, i, r];
        this.angleArr = [s, u, c, l];

        for (var p = 0; p <= 3; p++) {
            this.setCorners(p);
        }

        this.setFourCorner();
    }

    setPos(e, t, a) {
        var o = -n.ITEM_WIDTH * (a.row - e);
        var i = MatchGameConfig.ITEM_HEIGHT * (a.col - t);
        this.node.setPosition(cc.v3(o, i));
    }

    haveSamePos(e, t, a) {
        for (var o = 0; o < a.length; o++) {
            if (a[o].row == e && a[o].col == t) {
                return 1;
            }
        }
        return 0;
    }

    setCorners(e) {
        var t = 1 == this.direArr[e];
        var a = 1 == this.angleArr[e];
        var o = 1 == this.direArr[e + 1];

        if (3 == e) {
            o = 1 == this.direArr[0];
        }

        // if (a && t && o) {
        //     this.node.getChildByName("fill_" + e).active = true;
        // }

        if (!a || t || o) {
            this.node.getChildByName("jiao_" + e).active = true;
        }

        if (a && t && !o) {
            if (3 == e) {
                this.node.getChildByName("side_0_0").active = true;
            } else {
                this.node.getChildByName("side_" + (e + 1) + "_0").active = true;
            }
        }

        if (a && !t && o) {
            this.node.getChildByName("side_" + e + "_1").active = true;
        }

        // if (!a && t && o) {
        //     this.node.getChildByName("fill_" + e).active = true;
        // }

        if (a || t || !o) {
            this.node.getChildByName("side_" + e + "_1").active = true;
        }

        if (a || !t || o) {
            if (3 == e) {
                this.node.getChildByName("side_0_0").active = true;
            } else {
                this.node.getChildByName("side_" + (e + 1) + "_0").active = true;
            }
        }
    }

    setFourCorner() {
        if (!this.direArr[0] && this.angleArr[0] && this.direArr[1]) {
            this.outSides[0].active = true;
        }

        if (this.direArr[1] && !this.angleArr[1] && this.direArr[2]) {
            this.outSides[1].active = true;
        }

        if (this.direArr[2] && !this.angleArr[2] && this.direArr[3]) {
            this.outSides[2].active = true;
        }

        if (!this.direArr[0] && this.angleArr[3] && this.direArr[3]) {
            this.outSides[3].active = true;
        }
    }

    playDestoryAni() {
        this.cupSpine.active = false;
        l.playCupBreakAudio();
    }

    playShakeAni() {
        this.cupSpine.getComponent(sp.Skeleton).setAnimation(0, "idle_2", false);
    }
}